package com.zhaodui.sd.controller;
import com.zhaodui.order.entity.FxjOrderAttaEntity;
import com.zhaodui.order.entity.FxjOrderMainEntity;
import com.zhaodui.restapi.entity.FileMeta;
import com.zhaodui.sd.entity.SdCarinfoEntity;
import com.zhaodui.sd.service.SdCarinfoServiceI;
import com.zhaodui.sd.page.SdCarinfoPage;
import com.zhaodui.sd.entity.SdVendorEntity;
import com.zhaodui.sd.entity.SdPoPriceEntity;
import com.zhaodui.sd.entity.SdCarPicEntity;
import com.zhaodui.sd.entity.SdFkLogEntity;
import com.zhaodui.sd.entity.SdCustomerEntity;
import com.zhaodui.sd.entity.SdActivityEntity;
import com.zhaodui.sd.entity.SdPriceEntity;
import com.zhaodui.sd.entity.SdSkLogEntity;
import com.zhaodui.sd.entity.SdCsPlanEntity;
import com.zhaodui.sd.entity.SdCsActityEntity;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.models.auth.In;
import org.jeecgframework.core.util.*;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.TreeChildCount;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;

import java.io.OutputStream;

import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;
import org.apache.commons.lang3.StringUtils;
import org.jeecgframework.jwt.util.GsonUtil;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.jeecgframework.web.cgform.entity.upload.CgUploadEntity;
import org.jeecgframework.web.cgform.service.config.CgFormFieldServiceI;
import java.util.HashMap;

import static com.xiaoleilu.hutool.date.DateTime.now;

/**
 * @Title: Controller
 * @Description: 车辆主数据
 * @author onlineGenerator
 * @date 2019-02-24 17:35:09
 * @version V1.0   
 *
 */
@Api(value="SdCarinfo",description="车辆主数据",tags="sdCarinfoController")
@Controller
@RequestMapping("/sdCarinfoController")
public class SdCarinfoController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(SdCarinfoController.class);

	@Autowired
	private SdCarinfoServiceI sdCarinfoService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;
	@Autowired
	private CgFormFieldServiceI cgFormFieldService;

	/**
	 * 车辆主数据列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("com/zhaodui/sd/sdCarinfoList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(SdCarinfoEntity sdCarinfo,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SdCarinfoEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, sdCarinfo,request.getParameterMap());
		try{
		//自定义追加查询条件
		}catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.sdCarinfoService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除车辆主数据
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(SdCarinfoEntity sdCarinfo, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		sdCarinfo = systemService.getEntity(SdCarinfoEntity.class, sdCarinfo.getId());
		String message = "车辆主数据删除成功";
		try{
			sdCarinfoService.delMain(sdCarinfo);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "车辆主数据删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 批量删除车辆主数据
	 * 
	 * @return
	 */
	 @RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids,HttpServletRequest request){
		AjaxJson j = new AjaxJson();
		String message = "车辆主数据删除成功";
		try{
			for(String id:ids.split(",")){
				SdCarinfoEntity sdCarinfo = systemService.getEntity(SdCarinfoEntity.class,
				Integer.parseInt(id)
				);
				sdCarinfoService.delMain(sdCarinfo);
				systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
			}
		}catch(Exception e){
			e.printStackTrace();
			message = "车辆主数据删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 添加车辆主数据
	 * 
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(HttpServletRequest request,SdCarinfoPage sdCarinfoPage) {
		AjaxJson j = new AjaxJson();
		String message = "添加成功";
		try {
			sdCarinfoService.addMain(sdCarinfoPage);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "车辆主数据添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}
	/**
	 * 更新车辆主数据
	 * 
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(HttpServletRequest request,SdCarinfoPage sdCarinfoPage) {
		AjaxJson j = new AjaxJson();
		String message = "更新成功";
		try{
			sdCarinfoService.updateMain(sdCarinfoPage);
			systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
		}catch(Exception e){
			e.printStackTrace();
			message = "更新车辆主数据失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 车辆主数据新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(HttpServletRequest req) {
		return new ModelAndView("com/zhaodui/sd/sdCarinfo-add");
	}
	
	/**
	 * 车辆主数据编辑页面跳转
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(HttpServletRequest req) {
		String id = req.getParameter("id");
		SdCarinfoPage sdCarinfoPage = new SdCarinfoPage();
		if (StringUtil.isNotEmpty(id)) {
			SdCarinfoEntity sdCarinfo = sdCarinfoService.getEntity(SdCarinfoEntity.class,id);
			try {
				MyBeanUtils.copyBeanNotNull2Bean(sdCarinfo,sdCarinfoPage);
			} catch (Exception e) {
				e.printStackTrace();
			}
			//===================================================================================
			//获取参数
			Object carVin0 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-供应商
		    String hql0 = "from SdVendorEntity where 1 = 1 AND carVin = ? ";
		    List<SdVendorEntity> sdVendorEntityList = systemService.findHql(hql0,carVin0);
		    if(sdVendorEntityList==null || sdVendorEntityList.size()<=0){
		    	sdVendorEntityList = new ArrayList<SdVendorEntity>();
		    	sdVendorEntityList.add(new SdVendorEntity());
		    }
		    sdCarinfoPage.setSdVendorList(sdVendorEntityList);
			//===================================================================================
			//获取参数
			Object carVin1 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-采购价格明细
		    String hql1 = "from SdPoPriceEntity where 1 = 1 AND carVin = ? ";
		    List<SdPoPriceEntity> sdPoPriceEntityList = systemService.findHql(hql1,carVin1);
		    if(sdPoPriceEntityList==null || sdPoPriceEntityList.size()<=0){
		    	sdPoPriceEntityList = new ArrayList<SdPoPriceEntity>();
		    	sdPoPriceEntityList.add(new SdPoPriceEntity());
		    }
		    sdCarinfoPage.setSdPoPriceList(sdPoPriceEntityList);
			//===================================================================================
			//获取参数
			Object carVin2 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-车辆照片
		    String hql2 = "from SdCarPicEntity where 1 = 1 AND carVin = ? ";
		    List<SdCarPicEntity> sdCarPicEntityList = systemService.findHql(hql2,carVin2);
		    if(sdCarPicEntityList==null || sdCarPicEntityList.size()<=0){
		    	sdCarPicEntityList = new ArrayList<SdCarPicEntity>();
		    	sdCarPicEntityList.add(new SdCarPicEntity());
		    }
		    sdCarinfoPage.setSdCarPicList(sdCarPicEntityList);
			//===================================================================================
			//获取参数
			Object carVin3 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-付款信息
		    String hql3 = "from SdFkLogEntity where 1 = 1 AND carVin = ? ";
		    List<SdFkLogEntity> sdFkLogEntityList = systemService.findHql(hql3,carVin3);
		    if(sdFkLogEntityList==null || sdFkLogEntityList.size()<=0){
		    	sdFkLogEntityList = new ArrayList<SdFkLogEntity>();
		    	sdFkLogEntityList.add(new SdFkLogEntity());
		    }
		    sdCarinfoPage.setSdFkLogList(sdFkLogEntityList);
			//===================================================================================
			//获取参数
			Object carVin4 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-客户
		    String hql4 = "from SdCustomerEntity where 1 = 1 AND carVin = ? ";
		    List<SdCustomerEntity> sdCustomerEntityList = systemService.findHql(hql4,carVin4);
		    if(sdCustomerEntityList==null || sdCustomerEntityList.size()<=0){
		    	sdCustomerEntityList = new ArrayList<SdCustomerEntity>();
		    	sdCustomerEntityList.add(new SdCustomerEntity());
		    }
		    sdCarinfoPage.setSdCustomerList(sdCustomerEntityList);
			//===================================================================================
			//获取参数
			Object carVin5 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-售前活动
		    String hql5 = "from SdActivityEntity where 1 = 1 AND carVin = ? ";
		    List<SdActivityEntity> sdActivityEntityList = systemService.findHql(hql5,carVin5);
		    if(sdActivityEntityList==null || sdActivityEntityList.size()<=0){
		    	sdActivityEntityList = new ArrayList<SdActivityEntity>();
		    	sdActivityEntityList.add(new SdActivityEntity());
		    }
		    sdCarinfoPage.setSdActivityList(sdActivityEntityList);
			//===================================================================================
			//获取参数
			Object carVin6 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-销售价格明细
		    String hql6 = "from SdPriceEntity where 1 = 1 AND carVin = ? ";
		    List<SdPriceEntity> sdPriceEntityList = systemService.findHql(hql6,carVin6);
		    if(sdPriceEntityList==null || sdPriceEntityList.size()<=0){
		    	sdPriceEntityList = new ArrayList<SdPriceEntity>();
		    	sdPriceEntityList.add(new SdPriceEntity());
		    }
		    sdCarinfoPage.setSdPriceList(sdPriceEntityList);
			//===================================================================================
			//获取参数
			Object carVin7 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-收款信息
		    String hql7 = "from SdSkLogEntity where 1 = 1 AND carVin = ? ";
		    List<SdSkLogEntity> sdSkLogEntityList = systemService.findHql(hql7,carVin7);
		    if(sdSkLogEntityList==null || sdSkLogEntityList.size()<=0){
		    	sdSkLogEntityList = new ArrayList<SdSkLogEntity>();
		    	sdSkLogEntityList.add(new SdSkLogEntity());
		    }
		    sdCarinfoPage.setSdSkLogList(sdSkLogEntityList);
			//===================================================================================
			//获取参数
			Object carVin8 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-售后计划
		    String hql8 = "from SdCsPlanEntity where 1 = 1 AND carVin = ? ";
		    List<SdCsPlanEntity> sdCsPlanEntityList = systemService.findHql(hql8,carVin8);
		    if(sdCsPlanEntityList==null || sdCsPlanEntityList.size()<=0){
		    	sdCsPlanEntityList = new ArrayList<SdCsPlanEntity>();
		    	sdCsPlanEntityList.add(new SdCsPlanEntity());
		    }
		    sdCarinfoPage.setSdCsPlanList(sdCsPlanEntityList);
			//===================================================================================
			//获取参数
			Object carVin9 = sdCarinfo.getCarVin();
			//===================================================================================
		    //查询-售后活动
		    String hql9 = "from SdCsActityEntity where 1 = 1 AND carVin = ? ";
		    List<SdCsActityEntity> sdCsActityEntityList = systemService.findHql(hql9,carVin9);
		    if(sdCsActityEntityList==null || sdCsActityEntityList.size()<=0){
		    	sdCsActityEntityList = new ArrayList<SdCsActityEntity>();
		    	sdCsActityEntityList.add(new SdCsActityEntity());
		    }
		    sdCarinfoPage.setSdCsActityList(sdCsActityEntityList);
		}
		req.setAttribute("sdCarinfoPage", sdCarinfoPage);
		return new ModelAndView("com/zhaodui/sd/sdCarinfo-update");
	}
	
    /**
    * 导出excel
    *
    * @param request
    * @param response
    */
    @RequestMapping(params = "exportXls")
    public String exportXls(SdCarinfoEntity sdCarinfo,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid,ModelMap map) {
    	CriteriaQuery cq = new CriteriaQuery(SdCarinfoEntity.class, dataGrid);
    	//查询条件组装器
    	org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, sdCarinfo);
    	try{
    	//自定义追加查询条件
    	}catch (Exception e) {
    		throw new BusinessException(e.getMessage());
    	}
    	cq.add();
    	List<SdCarinfoEntity> list=this.sdCarinfoService.getListByCriteriaQuery(cq, false);
    	List<SdCarinfoPage> pageList=new ArrayList<SdCarinfoPage>();
        if(list!=null&&list.size()>0){
        	for(SdCarinfoEntity entity:list){
        		try{
        			SdCarinfoPage page=new SdCarinfoPage();
        			MyBeanUtils.copyBeanNotNull2Bean(entity,page);
           			Object carVin0 = entity.getCarVin();
					String hql0 = "from SdVendorEntity where 1 = 1 AND carVin = ? ";
					List<SdVendorEntity> sdVendorEntityList = systemService.findHql(hql0,carVin0);
					page.setSdVendorList(sdVendorEntityList);
           			Object carVin1 = entity.getCarVin();
					String hql1 = "from SdPoPriceEntity where 1 = 1 AND carVin = ? ";
					List<SdPoPriceEntity> sdPoPriceEntityList = systemService.findHql(hql1,carVin1);
					page.setSdPoPriceList(sdPoPriceEntityList);
           			Object carVin2 = entity.getCarVin();
					String hql2 = "from SdCarPicEntity where 1 = 1 AND carVin = ? ";
					List<SdCarPicEntity> sdCarPicEntityList = systemService.findHql(hql2,carVin2);
					page.setSdCarPicList(sdCarPicEntityList);
           			Object carVin3 = entity.getCarVin();
					String hql3 = "from SdFkLogEntity where 1 = 1 AND carVin = ? ";
					List<SdFkLogEntity> sdFkLogEntityList = systemService.findHql(hql3,carVin3);
					page.setSdFkLogList(sdFkLogEntityList);
           			Object carVin4 = entity.getCarVin();
					String hql4 = "from SdCustomerEntity where 1 = 1 AND carVin = ? ";
					List<SdCustomerEntity> sdCustomerEntityList = systemService.findHql(hql4,carVin4);
					page.setSdCustomerList(sdCustomerEntityList);
           			Object carVin5 = entity.getCarVin();
					String hql5 = "from SdActivityEntity where 1 = 1 AND carVin = ? ";
					List<SdActivityEntity> sdActivityEntityList = systemService.findHql(hql5,carVin5);
					page.setSdActivityList(sdActivityEntityList);
           			Object carVin6 = entity.getCarVin();
					String hql6 = "from SdPriceEntity where 1 = 1 AND carVin = ? ";
					List<SdPriceEntity> sdPriceEntityList = systemService.findHql(hql6,carVin6);
					page.setSdPriceList(sdPriceEntityList);
           			Object carVin7 = entity.getCarVin();
					String hql7 = "from SdSkLogEntity where 1 = 1 AND carVin = ? ";
					List<SdSkLogEntity> sdSkLogEntityList = systemService.findHql(hql7,carVin7);
					page.setSdSkLogList(sdSkLogEntityList);
           			Object carVin8 = entity.getCarVin();
					String hql8 = "from SdCsPlanEntity where 1 = 1 AND carVin = ? ";
					List<SdCsPlanEntity> sdCsPlanEntityList = systemService.findHql(hql8,carVin8);
					page.setSdCsPlanList(sdCsPlanEntityList);
           			Object carVin9 = entity.getCarVin();
					String hql9 = "from SdCsActityEntity where 1 = 1 AND carVin = ? ";
					List<SdCsActityEntity> sdCsActityEntityList = systemService.findHql(hql9,carVin9);
					page.setSdCsActityList(sdCsActityEntityList);
            		pageList.add(page);
            	}catch(Exception e){
            		logger.info(e.getMessage());
            	}
            }
        }
        map.put(NormalExcelConstants.FILE_NAME,"车辆主数据");
        map.put(NormalExcelConstants.CLASS,SdCarinfoPage.class);
        map.put(NormalExcelConstants.PARAMS,new ExportParams("车辆主数据列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(), "导出信息"));
        map.put(NormalExcelConstants.DATA_LIST,pageList);
        return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

    /**
	 * 通过excel导入数据
	 * @param request
	 * @param
	 * @return
	 */
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(2);
			params.setNeedSave(true);
			try {
				List<SdCarinfoPage> list =  ExcelImportUtil.importExcel(file.getInputStream(), SdCarinfoPage.class, params);
				for (SdCarinfoPage page : list) {
		            sdCarinfoService.addMain(page);
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			}finally{
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			}
			return j;
	}
	/**
	* 导出excel 使模板
	*/
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(ModelMap map) {
		map.put(NormalExcelConstants.FILE_NAME,"车辆主数据");
		map.put(NormalExcelConstants.CLASS,SdCarinfoPage.class);
		map.put(NormalExcelConstants.PARAMS,new ExportParams("车辆主数据列表", "导出人:"+ ResourceUtil.getSessionUser().getRealName(),
		"导出信息"));
		map.put(NormalExcelConstants.DATA_LIST,new ArrayList());
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}
	/**
	* 导入功能跳转
	*
	* @return
	*/
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name", "sdCarinfoController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}


    @RequestMapping(value="/list/{username}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆主数据列表信息",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdCarinfoPage>> list(@PathVariable("username") String username, @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		CriteriaQuery query = new CriteriaQuery(SdCarinfoEntity.class);
        try {
			if (StringUtil.isNotEmpty(request.getParameter("carStatus"))) {
				query.like("carStatus", "%" + request.getParameter("carStatus") + "%");
			}
            if (StringUtil.isNotEmpty(request.getParameter("carYear"))) {
                query.like("carYear", "%" + request.getParameter("carYear") + "%");
            }
            if (StringUtil.isNotEmpty(request.getParameter("carType"))) {
                query.like("carType", "%" + request.getParameter("carType") + "%");
            }
            if (StringUtil.isNotEmpty(request.getParameter("carNo"))) {
                query.like("carNo", "%" + request.getParameter("carNo") + "%");
            }
            if (StringUtil.isNotEmpty(request.getParameter("carName"))) {
                query.like("carName", "%" + request.getParameter("carName") + "%");
            }
            if (StringUtil.isNotEmpty(request.getParameter("carVin"))) {
                query.like("carVin", "%" + request.getParameter("carVin") + "%");
            }
			if (StringUtil.isNotEmpty(request.getParameter("carChezhu1"))) {
				query.like("carChezhu1", "%" + request.getParameter("carChezhu1") + "%");
			}
			if (StringUtil.isNotEmpty(request.getParameter("carChezhu2"))) {
				query.like("carChezhu2", "%" + request.getParameter("carChezhu2") + "%");
			}
            if (StringUtil.isNotEmpty(request.getParameter("carLsnumo"))) {
                query.like("carLsnumo", "%" + request.getParameter("carLsnumo") + "%");
            }

        }catch (Exception   e){

        }

            query.add();

            List<SdCarinfoEntity> listsize = this.sdCarinfoService.getListByCriteriaQuery(query,false);

        query.setCurPage(pageNumber<=0?1:pageNumber);
        query.setPageSize(pageSize<1?1:pageSize);
        query.add();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("createDate", "desc");
        query.setOrder(map);
		List<SdCarinfoEntity> list = this.sdCarinfoService.getListByCriteriaQuery(query,true);
    	List<SdCarinfoPage> pageList=new ArrayList<SdCarinfoPage>();
        if(list!=null&&list.size()>0){
        	for(SdCarinfoEntity entity:list){
        		try{
        			SdCarinfoPage page=new SdCarinfoPage();
        		   MyBeanUtils.copyBeanNotNull2Bean(entity,page);
//					Object carVin0 = entity.getCarVin();
////					Object carVin1 = entity.getCarVin();
////					Object carVin2 = entity.getCarVin();
////					Object carVin3 = entity.getCarVin();
////					Object carVin4 = entity.getCarVin();
////					Object carVin5 = entity.getCarVin();
////					Object carVin6 = entity.getCarVin();
////					Object carVin7 = entity.getCarVin();
////					Object carVin8 = entity.getCarVin();
////					Object carVin9 = entity.getCarVin();
////				    String hql0 = "from SdVendorEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdVendorEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin0);
////				    page.setSdVendorList(sdVendorOldList);
////				    String hql1 = "from SdPoPriceEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdPoPriceEntity> sdPoPriceOldList = this.sdCarinfoService.findHql(hql1,carVin1);
////				    page.setSdPoPriceList(sdPoPriceOldList);
////				    String hql2 = "from SdCarPicEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdCarPicEntity> sdCarPicOldList = this.sdCarinfoService.findHql(hql2,carVin2);
////				    page.setSdCarPicList(sdCarPicOldList);
////				    String hql3 = "from SdFkLogEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdFkLogEntity> sdFkLogOldList = this.sdCarinfoService.findHql(hql3,carVin3);
////				    page.setSdFkLogList(sdFkLogOldList);
////				    String hql4 = "from SdCustomerEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdCustomerEntity> sdCustomerOldList = this.sdCarinfoService.findHql(hql4,carVin4);
////				    page.setSdCustomerList(sdCustomerOldList);
////				    String hql5 = "from SdActivityEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdActivityEntity> sdActivityOldList = this.sdCarinfoService.findHql(hql5,carVin5);
////				    page.setSdActivityList(sdActivityOldList);
////				    String hql6 = "from SdPriceEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdPriceEntity> sdPriceOldList = this.sdCarinfoService.findHql(hql6,carVin6);
////				    page.setSdPriceList(sdPriceOldList);
////				    String hql7 = "from SdSkLogEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdSkLogEntity> sdSkLogOldList = this.sdCarinfoService.findHql(hql7,carVin7);
////				    page.setSdSkLogList(sdSkLogOldList);
////				    String hql8 = "from SdCsPlanEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdCsPlanEntity> sdCsPlanOldList = this.sdCarinfoService.findHql(hql8,carVin8);
////				    page.setSdCsPlanList(sdCsPlanOldList);
////				    String hql9 = "from SdCsActityEntity where 1 = 1 AND carVin = ? ";
////	    			List<SdCsActityEntity> sdCsActityOldList = this.sdCarinfoService.findHql(hql9,carVin9);
////				    page.setSdCsActityList(sdCsActityOldList);
            		pageList.add(page);
            	}catch(Exception e){
            		logger.info(e.getMessage());
            	}
            }
        }
		return Result.success(pageList,(long) listsize.size());
	}


	@RequestMapping(value="/listdetail0/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆0",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdVendorEntity>> listdetail0(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
	    String hql0 = "from SdVendorEntity where 1 = 1 AND carVin = ? ";
	    List<SdVendorEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
	    listsizec = sdVendorOldList.size();
	    return Result.success(sdVendorOldList, listsizec);
	}


	@RequestMapping(value="/listdetail1/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆1",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdPoPriceEntity>> listdetail1(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdPoPriceEntity where 1 = 1 AND carVin = ? ";
		List<SdPoPriceEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail2/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆2",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdCarPicEntity>> listdetail2(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdCarPicEntity where 1 = 1 AND carVin = ? ";
		List<SdCarPicEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail3/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆0",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdFkLogEntity>> listdetail3(@PathVariable("carVin") String carVin ,@RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdFkLogEntity where 1 = 1 AND carVin = ? ";
		List<SdFkLogEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail4/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆4",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdCustomerEntity>> listdetail4(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdCustomerEntity where 1 = 1 AND carVin = ? ";
		List<SdCustomerEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail5/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆5",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdActivityEntity>> listdetail5(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdActivityEntity where 1 = 1 AND carVin = ? ";
		List<SdActivityEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail6/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆6",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdPriceEntity>> listdetail6(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdPriceEntity where 1 = 1 AND carVin = ? ";
		List<SdPriceEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail7/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆7",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdSkLogEntity>> listdetail7(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdSkLogEntity where 1 = 1 AND carVin = ? ";
		List<SdSkLogEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail8/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆8",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdCsPlanEntity>> listdetail8(@PathVariable("carVin") String carVin,  @RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdCsPlanEntity where 1 = 1 AND carVin = ? ";
		List<SdCsPlanEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

	@RequestMapping(value="/listdetail9/{carVin}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="车辆9",produces="application/json",httpMethod="GET")
	public ResponseMessage<List<SdCsActityEntity>> listdetail9(@PathVariable("carVin") String carVin,@RequestParam int pageNumber, @RequestParam int pageSize, HttpServletRequest request) {
		if(pageSize>Globals.MAX_PAGESIZE){
			return Result.error("每页请求不能超过" + Globals.MAX_PAGESIZE + "条");
		}
		long listsizec = 0;
		String hql0 = "from SdCsActityEntity where 1 = 1 AND carVin = ? ";
		List<SdCsActityEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin);
		listsizec = sdVendorOldList.size();
		return Result.success(sdVendorOldList, listsizec);
	}

    @RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value="根据ID获取车辆主数据信息",notes="根据ID获取车辆主数据信息",httpMethod="GET",produces="application/json")
    public ResponseMessage<?> getdel(@ApiParam(required=true,name="id",value="id")@PathVariable("id") String id, HttpServletRequest request) {
            String  deltype = request.getParameter("deltype");
try{
           if("0".equals(deltype)){
            String hql0 = "from SdVendorEntity where 1 = 1 AND id = ? ";
            List<SdVendorEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,Integer.parseInt(id));
            systemService.deleteAllEntitie(sdVendorOldList);
              }
    if("1".equals(deltype)){

            String hql1 = "from SdPoPriceEntity where 1 = 1 AND id = ? ";
            List<SdPoPriceEntity> sdPoPriceOldList = this.sdCarinfoService.findHql(hql1,Integer.parseInt(id));
        systemService.deleteAllEntitie(sdPoPriceOldList);

    }
    if("2".equals(deltype)){
            String hql2 = "from SdCarPicEntity where 1 = 1 AND id = ? ";
            List<SdCarPicEntity> sdCarPicOldList = this.sdCarinfoService.findHql(hql2,id);
        systemService.deleteAllEntitie(sdCarPicOldList);
    }
    if("3".equals(deltype)){
            String hql3 = "from SdFkLogEntity where 1 = 1 AND id = ? ";
            List<SdFkLogEntity> sdFkLogOldList = this.sdCarinfoService.findHql(hql3,id);
        systemService.deleteAllEntitie(sdFkLogOldList);
    }
    if("4".equals(deltype)){
            String hql4 = "from SdCustomerEntity where 1 = 1 AND id = ? ";
            List<SdCustomerEntity> sdCustomerOldList = this.sdCarinfoService.findHql(hql4,Integer.parseInt(id));
        systemService.deleteAllEntitie(sdCustomerOldList);
    }
    if("5".equals(deltype)){
            String hql5 = "from SdActivityEntity where 1 = 1 AND id = ? ";
            List<SdActivityEntity> sdActivityOldList = this.sdCarinfoService.findHql(hql5,Integer.parseInt(id));
        systemService.deleteAllEntitie(sdActivityOldList);
    }
    if("6".equals(deltype)){
            String hql6 = "from SdPriceEntity where 1 = 1 AND id = ? ";
            List<SdPriceEntity> sdPriceOldList = this.sdCarinfoService.findHql(hql6,Integer.parseInt(id));
        systemService.deleteAllEntitie(sdPriceOldList);
    }
    if("7".equals(deltype)){
            String hql7 = "from SdSkLogEntity where 1 = 1 AND id = ? ";
            List<SdSkLogEntity> sdSkLogOldList = this.sdCarinfoService.findHql(hql7,id);
        systemService.deleteAllEntitie(sdSkLogOldList);    }
    if("8".equals(deltype)){

            String hql8 = "from SdCsPlanEntity where 1 = 1 AND id = ? ";
            List<SdCsPlanEntity> sdCsPlanOldList = this.sdCarinfoService.findHql(hql8,Integer.parseInt(id));
        systemService.deleteAllEntitie(sdCsPlanOldList);
    }
    if("9".equals(deltype)) {
        String hql9 = "from SdCsActityEntity where 1 = 1 AND id = ? ";
        List<SdCsActityEntity> sdCsActityOldList = this.sdCarinfoService.findHql(hql9, Integer.parseInt(id));
        systemService.deleteAllEntitie(sdCsActityOldList);
    }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.success("删除成功");
    }
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value="根据ID获取车辆主数据信息",notes="根据ID获取车辆主数据信息",httpMethod="GET",produces="application/json")
	public ResponseMessage<?> get(@ApiParam(required=true,name="id",value="ID")@PathVariable("id") String id) {
		SdCarinfoEntity task = sdCarinfoService.get(SdCarinfoEntity.class, Integer.parseInt(id));
		if (task == null) {
			return Result.error("根据ID获取车辆主数据信息为空");
		}
		SdCarinfoPage page = new SdCarinfoPage();
		try {
			MyBeanUtils.copyBeanNotNull2Bean(task, page);
				Object carVin0 = task.getCarVin();
				Object carVin1 = task.getCarVin();
				Object carVin2 = task.getCarVin();
				Object carVin3 = task.getCarVin();
				Object carVin4 = task.getCarVin();
				Object carVin5 = task.getCarVin();
				Object carVin6 = task.getCarVin();
				Object carVin7 = task.getCarVin();
				Object carVin8 = task.getCarVin();
				Object carVin9 = task.getCarVin();
		    String hql0 = "from SdVendorEntity where 1 = 1 AND carVin = ? ";
			List<SdVendorEntity> sdVendorOldList = this.sdCarinfoService.findHql(hql0,carVin0);
		    page.setSdVendorList(sdVendorOldList);
			
			
		    String hql1 = "from SdPoPriceEntity where 1 = 1 AND carVin = ? ";
			List<SdPoPriceEntity> sdPoPriceOldList = this.sdCarinfoService.findHql(hql1,carVin1);
		    page.setSdPoPriceList(sdPoPriceOldList);
			
			
		    String hql2 = "from SdCarPicEntity where 1 = 1 AND carVin = ? ";
			List<SdCarPicEntity> sdCarPicOldList = this.sdCarinfoService.findHql(hql2,carVin2);
		    page.setSdCarPicList(sdCarPicOldList);
			
			
		    String hql3 = "from SdFkLogEntity where 1 = 1 AND carVin = ? ";
			List<SdFkLogEntity> sdFkLogOldList = this.sdCarinfoService.findHql(hql3,carVin3);
		    page.setSdFkLogList(sdFkLogOldList);
			
			
		    String hql4 = "from SdCustomerEntity where 1 = 1 AND carVin = ? ";
			List<SdCustomerEntity> sdCustomerOldList = this.sdCarinfoService.findHql(hql4,carVin4);
		    page.setSdCustomerList(sdCustomerOldList);
			
			
		    String hql5 = "from SdActivityEntity where 1 = 1 AND carVin = ? ";
			List<SdActivityEntity> sdActivityOldList = this.sdCarinfoService.findHql(hql5,carVin5);
		    page.setSdActivityList(sdActivityOldList);
			
			
		    String hql6 = "from SdPriceEntity where 1 = 1 AND carVin = ? ";
			List<SdPriceEntity> sdPriceOldList = this.sdCarinfoService.findHql(hql6,carVin6);
		    page.setSdPriceList(sdPriceOldList);
			
			
		    String hql7 = "from SdSkLogEntity where 1 = 1 AND carVin = ? ";
			List<SdSkLogEntity> sdSkLogOldList = this.sdCarinfoService.findHql(hql7,carVin7);
		    page.setSdSkLogList(sdSkLogOldList);
			
			
		    String hql8 = "from SdCsPlanEntity where 1 = 1 AND carVin = ? ";
			List<SdCsPlanEntity> sdCsPlanOldList = this.sdCarinfoService.findHql(hql8,carVin8);
		    page.setSdCsPlanList(sdCsPlanOldList);
			
			
		    String hql9 = "from SdCsActityEntity where 1 = 1 AND carVin = ? ";
			List<SdCsActityEntity> sdCsActityOldList = this.sdCarinfoService.findHql(hql9,carVin9);
		    page.setSdCsActityList(sdCsActityOldList);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.success(page);
	}

    @RequestMapping(value = "/cre/0",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建0")
    public ResponseMessage<?> create0(@ApiParam(name="0")@RequestBody SdVendorEntity SdVendorEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdVendorEntity>> failures = validator.validate(SdVendorEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdVendorEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }

    @RequestMapping(value = "/cre/1",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建1")
    public ResponseMessage<?> create1(@ApiParam(name="1")@RequestBody SdPoPriceEntity SdPoPriceEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdPoPriceEntity>> failures = validator.validate(SdPoPriceEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdPoPriceEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/2",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建2")
    public ResponseMessage<?> create2(@ApiParam(name="2")@RequestBody SdCarPicEntity SdCarPicEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdCarPicEntity>> failures = validator.validate(SdCarPicEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdCarPicEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/3",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建3")
    public ResponseMessage<?> create3(@ApiParam(name="3")@RequestBody SdFkLogEntity SdFkLogEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdFkLogEntity>> failures = validator.validate(SdFkLogEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdFkLogEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/4",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建4")
    public ResponseMessage<?> create4(@ApiParam(name="4")@RequestBody SdCustomerEntity SdCustomerEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdCustomerEntity>> failures = validator.validate(SdCustomerEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdCustomerEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/5",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建5")
    public ResponseMessage<?> create5(@ApiParam(name="5")@RequestBody SdActivityEntity SdActivityEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdActivityEntity>> failures = validator.validate(SdActivityEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdActivityEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/6",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建6")
    public ResponseMessage<?> create6(@ApiParam(name="6")@RequestBody SdPriceEntity SdPriceEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdPriceEntity>> failures = validator.validate(SdPriceEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdPriceEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/7",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建7")
    public ResponseMessage<?> create7(@ApiParam(name="7")@RequestBody SdSkLogEntity SdSkLogEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdSkLogEntity>> failures = validator.validate(SdSkLogEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdSkLogEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/8",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建8")
    public ResponseMessage<?> create1(@ApiParam(name="8")@RequestBody SdCsPlanEntity SdCsPlanEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdCsPlanEntity>> failures = validator.validate(SdCsPlanEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdCsPlanEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }
    @RequestMapping(value = "/cre/9",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    @ApiOperation(value="创建9")
    public ResponseMessage<?> create9(@ApiParam(name="9")@RequestBody SdCsActityEntity SdCsActityEntity, UriComponentsBuilder uriBuilder) {
        Set<ConstraintViolation<SdCsActityEntity>> failures = validator.validate(SdCsActityEntity);
        if (!failures.isEmpty()) {
            return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
        }
        try{
            systemService.save(SdCsActityEntity);
        }catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存失败");
        }
        return Result.success();
    }


    @RequestMapping(value="/uploadcarpic", method = RequestMethod.POST)
    @ApiOperation(value="图片上传",notes="图片上传",httpMethod="POST",produces="application/json")
    public @ResponseBody
    LinkedList<FileMeta> upload(@RequestParam(value="filetype", required=false) String filetype, @RequestParam(value="carVin", required=false) String carVin, MultipartHttpServletRequest request, HttpServletResponse response) {
        LinkedList<FileMeta> files = new LinkedList<FileMeta>();
        String showAndDownUrl = ResourceUtil.getConfigByName("showAndDownUrl");
        String imgserver = ResourceUtil.getConfigByName("img.server");
        FileMeta fileMeta = null;
        //1. build an iterator
        Iterator<String> itr =  request.getFileNames();
        MultipartFile mpf = null;
        String filest = DateUtils.date2Str(DateUtils.yyyymmddhhmmss);
        //2. get each file
        String savaPath= ResourceUtil.getConfigByName("webUploadpath");
        String  filename = "";
        String  filenameweb = "";
        while(itr.hasNext()){

            //2.1 get next MultipartFile
            mpf = request.getFile(itr.next());
            System.out.println(mpf.getOriginalFilename() +" uploaded! "+files.size());

            //2.2 if files > 10 remove the first from the list

            filename = filest + File.separator+mpf.getOriginalFilename();
            filenameweb = filest + "/"+mpf.getOriginalFilename();

            SdCarPicEntity sdCarPicEntity  = new SdCarPicEntity();
            sdCarPicEntity.setCarVin(carVin);
            sdCarPicEntity.setCreateDate(now());
            sdCarPicEntity.setPicName(filest+File.separator+mpf.getOriginalFilename());
            sdCarPicEntity.setPicSize(mpf.getSize()/1024+" KB");
            sdCarPicEntity.setBpmStatus("1");
            sdCarPicEntity.setPicType(filetype);
            sdCarPicEntity.setPicUrl(imgserver+showAndDownUrl+filenameweb);
            String fileId = systemService.save(sdCarPicEntity).toString();
            //2.3 create new fileMeta
            fileMeta = new FileMeta();
            fileMeta.setFileId(fileId);
            fileMeta.setFileName(mpf.getOriginalFilename());
            fileMeta.setFileSize(mpf.getSize()/1024+" KB");
            fileMeta.setFileType(mpf.getContentType());
            fileMeta.setFileUrl(sdCarPicEntity.getPicUrl());
            try {
                fileMeta.setBytes(mpf.getBytes());
                // copy file to local disk (make sure the path "e.g. D:/temp/files" exists)
                String ctxPath=savaPath;

                File file = new File(ctxPath+File.separator+filest);
                if (!file.exists()) {
                    file.mkdirs();// 创建文件根目录
                }
                FileCopyUtils.copy(mpf.getBytes(), new FileOutputStream(ctxPath+File.separator+filename));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //2.4 add to files
            files.add(fileMeta);
        }

        return files;
    }



        @RequestMapping(value = "/{username}",method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="车辆主数据")
	public ResponseMessage<?> create(@ApiParam(name="username",value="username",required=true)@PathVariable("username") String username,@ApiParam(name="车辆主数据对象")@RequestBody SdCarinfoPage sdCarinfoPage, UriComponentsBuilder uriBuilder) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<SdCarinfoPage>> failures = validator.validate(sdCarinfoPage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}
		try{
			if(StringUtil.isNotEmpty(sdCarinfoPage.getId())){
				sdCarinfoPage.setUpdateBy(username);
				sdCarinfoService.updateMain(sdCarinfoPage);
			}else{
				sdCarinfoPage.setCreateBy(username);
				sdCarinfoService.addMain(sdCarinfoPage);
			}
		}catch(Exception e){
            logger.info(e.getMessage());
            return Result.error("保存车辆主数据失败");
        }
		return Result.success();
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	@ApiOperation(value="更新车辆主数据",notes="更新车辆主数据")
	public ResponseMessage<?> update(@RequestBody SdCarinfoPage sdCarinfoPage) {
		//调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<SdCarinfoPage>> failures = validator.validate(sdCarinfoPage);
		if (!failures.isEmpty()) {
			return Result.error(JSONArray.toJSONString(BeanValidators.extractPropertyAndMessage(failures)));
		}
		try {
			sdCarinfoService.updateMain(sdCarinfoPage);
		} catch (Exception e) {
			logger.info(e.getMessage());
	        return Result.error("更新车辆主数据失败");
		}
		//按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return Result.success();
	}
	
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	@ApiOperation(value="删除车辆主数据")
	public ResponseMessage<?> delete(@ApiParam(name="id",value="ID",required=true)@PathVariable("id") String id) {
		logger.info("delete[{}]" + id);
		// 验证
		if (StringUtils.isEmpty(id)) {
			return Result.error("ID不能为空");
		}
		try {
			SdCarinfoEntity sdCarinfo = sdCarinfoService.get(SdCarinfoEntity.class, id);
			sdCarinfoService.delMain(sdCarinfo);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error("车辆主数据删除失败");
		}

		return Result.success();
	}
	
	/**
	 * 获取文件附件信息
	 * 
	 * @param id sdCarinfo主键id
	 */
	@RequestMapping(params = "getFiles")
	@ResponseBody
	public AjaxJson getFiles(String id){
		List<CgUploadEntity> uploadBeans = cgFormFieldService.findByProperty(CgUploadEntity.class, "cgformId", id);
		List<Map<String,Object>> files = new ArrayList<Map<String,Object>>(0);
		for(CgUploadEntity b:uploadBeans){
			String title = b.getAttachmenttitle();//附件名
			String fileKey = b.getId();//附件主键
			String path = b.getRealpath();//附件路径
			String field = b.getCgformField();//表单中作为附件控件的字段
			Map<String, Object> file = new HashMap<String, Object>();
			file.put("title", title);
			file.put("fileKey", fileKey);
			file.put("path", path);
			file.put("field", field==null?"":field);
			files.add(file);
		}
		AjaxJson j = new AjaxJson();
		j.setObj(files);
		return j;
	}
}
